// pages/vote/new/new.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    start_date: '2018-01-01',
    start_time: '12:00',
    end_date: '2020-01-01',
    end_time: '12:00',
    dimen: ["创意", "实现程度", "团队合作"]
  },

  plus: function () {
    var dimen_temp = this.data.dimen
    dimen_temp.push("打分项")

    this.setData({
      dimen: dimen_temp
    })
  },

  substract: function (e) {
    var dimen_temp = this.data.dimen
    var index = e.currentTarget.dataset.index

    dimen_temp.splice(index, 1)

    this.setData({
      dimen: dimen_temp
    })
  },

  dimen_input: function (e) {
    var index = e.currentTarget.dataset.index
    var title = e.detail.value

    var dimen_temp = this.data.dimen
    dimen_temp[index] = title

    this.setData({
      dimen: dimen_temp
    })
  },

  formSubmit: function (e) {
    var that = this
    console.log(e.detail.value);
    var list_obj = e.detail.value

    for (var item in list_obj) {
      if (list_obj[item] == "") {
        wx.showModal({
          title: '哎呀～',
          content: "有的选项没有填呢～",
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        return
      } else if (isNaN(list_obj.votes_for_student) || isNaN(list_obj.votes_for_teacher) || isNaN(list_obj.weight_for_teacher)) {
        wx.showModal({
          title: '哎呀～',
          content: "票数和权重必须为数字～",
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        return
      }
    }


    //检查马拉松名字是否唯一
    wx.request({
      url: getApp().globalData.my_server + "/hackson/check_id_for_marathon",
      data: {
        name: e.detail.value.name
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res)
        if (res.data.error_no != 0) {
          wx.showModal({
            title: '哎呀～',
            content: res.data.error_msg,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.error_no == 0) {
          //创建马拉松
          wx.request({
            url: getApp().globalData.my_server + "/hackson/new_marathon",
            data: {
              name: e.detail.value.name,
              start_time: e.detail.value.start_date + " " + e.detail.value.start_time,
              stop_time: e.detail.value.end_date + " " + e.detail.value.end_time,
              address: e.detail.value.address,
              organizer: e.detail.value.organizer,
              participants: e.detail.value.participants,
              detail: e.detail.value.detail,
              votes_for_teacher: e.detail.value.votes_for_teacher,
              weight_for_teacher: e.detail.value.weight_for_teacher,
              votes_for_student: e.detail.value.votes_for_student,
              dimensions_for_vote: that.data.dimen.toString()
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
              console.log(res)
              if (res.data.error_no == 2) {
                wx.showModal({
                  title: '哎呀～',
                  content: res.data.error_msg,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              } else if (res.data.error_no == 0) {
                wx.redirectTo({
                  url: '../hackson/hackson',
                })
              }
            },
            fail: function (res) {
              wx.showModal({
                title: '哎呀～',
                content: '创建马拉松失败',
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
              console.log("fail!", res)
            },
            complete: function (res) {

            }
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '哎呀～',
          content: '检查马拉松名字是否唯一失败',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("fail!", res)
      },
      complete: function (res) {

      }
    })

  },

  bindStartDateChange: function (e) {
    this.setData({
      start_date: e.detail.value
    })
  },

  bindEndDateChange: function (e) {
    this.setData({
      end_date: e.detail.value
    })
  },

  bindStartTimeChange: function (e) {
    this.setData({
      start_time: e.detail.value
    })
  },

  bindEndTimeChange: function (e) {
    this.setData({
      end_time: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})